/*
 * Copyright (C) 2010 Juan José Ortilles jortilles@gmail.com
 *
 * This program is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation; either version 3 of the License, or 
 * any later version.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 *
 */
package net.juantxu.pentaho.launcher.ejecutores;

import java.io.File;
import java.util.Properties;

import net.juantxu.pentaho.launcher.utilidades.CargaProperties;
import net.juantxu.pentaho.launcher.utilidades.CreaBat;
import net.juantxu.pentaho.launcher.utilidades.ValidaEjecutableNix;
import net.juantxu.pentaho.launcher.utilidades.ValidaEjecutableWin;

import org.apache.log4j.Logger;

public class EjecutaWeka implements EjecutaPentaho  {
	static Logger log = Logger.getLogger(EjecutaWeka.class);
	
	public int lanza(){
		String os = "";
		int res = 0;
		log.debug("lanzando  weka");
		os =System.getProperty("os.name");
		log.debug("sistema operativo :".concat(os));	

		if (os.toLowerCase().contains("win")){
			res = ejecutaWin();
		}else{
			log.debug("es un sistema *NIX");
			res = ejecutaNix();
		}
		return res;
	}

	public int ejecutaWin(){
		int res = 0;
		Properties prop = new CargaProperties().Carga();
		log.debug("la ruta es"+prop.getProperty("wekaRelativePath"));
		File miPath = new File( prop.getProperty("wekaRelativePath"));
		log.debug("Ejecutando en :" + miPath.getAbsolutePath());
		if (  new ValidaEjecutableWin().valida(prop.getProperty("wekaRelativePath")+"\\weka.jar") ){
		new CreaBat(prop.getProperty("wekaRelativePath"),
		"javaw -classpath weka.jar;$CLASSPATH weka.gui.Main", "tmprunweka.bat");
		Runtime r = Runtime.getRuntime();
		try {
			r.exec("cmd.exe /K  start tmprunweka.bat ");
			//r.exec("javaw -classpath weka.jar;$CLASSPATH weka.gui.Mains", null, miPath );
			 Thread.sleep(3000);
		} catch (Exception e) {
			e.printStackTrace();
			res=1;
		}
		}else{
			//no se encuetra el ejecutable
			log.error("no se encuentra el ejecutable");
			res=7;
		}
		return res;
	}
	public int ejecutaNix(){
		Properties prop = new CargaProperties().Carga();
		int res = 0;
		File miPath = new File( prop.getProperty("wekaRelativePath"));
		log.debug("Ejecutando en :" + miPath.getAbsolutePath());
		// no tiene por que ser ejecutable.
		if ( new File(prop.getProperty("wekaRelativePath")+ "/weka.jar").exists() ){
			Runtime r = Runtime.getRuntime();
			try {
				r.exec("java -jar weka.jar", null, miPath );
				 Thread.sleep(3000);
			} catch (Exception e) {
				e.printStackTrace();
				res=1;
			}
		}else{
			//no se encuetra el ejecutable
			log.error("no se encuentra el ejecutable");
			log.error( new File(prop.getProperty("wekaRelativePath")+"/weka.jar").getAbsolutePath() );
			log.error( new File(prop.getProperty("wekaRelativePath")+"/weka.jar").exists());
			res=7;
		}
		
		return res;
	}
}
